const lbbTaskService = require("../service/task-service");

let calendar;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    startDate: '2021-01-01',
    showLevel: false,
    levels: [
      { level: 1, text: '10台及以下' },
      { level: 2, text: '11-20台' },
      { level: 3, text: '21-30台' },
      { level: 4, text: '31-40台' },
      { level: 5, text: '41-50台' },
      { level: 6, text: '51-60台' },
      { level: 7, text: '61-70台' },
      { level: 8, text: '71及以上' },
    ],
    selLevelIndex: 2, // 当前选中设备数量
    defLevelIndex: 0,
    refreshedPickup: true,
  },

  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getTaskPhotoTimeConfById(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(event) {
    console.log("stepDay=",event.detail);
    this.data.stepDay = event.detail
  },
  showLevelPicker() {
    this.setData({
      refreshedPickup: false,
      defLevelIndex: this.data.selLevelIndex != null ? this.data.selLevelIndex : 0,
    })
    this.setData({
      refreshedPickup: true,
      showLevel: true,
    })
  },
  onLevelConfirm(event) {
    this.setData({
      selLevelIndex: event.detail.index,
      showLevel: false,
    })
  },
  onLevelClose() {
    this.setData({
      showLevel: false,
    })
  },
  onLevelCancel() {
    this.setData({
      showLevel: false,
    })
  },

  getTaskPhotoTimeConfById(id) {
    let that = this
    this.data.confId = id
    lbbTaskService.getTaskTimeConfById({
      id:id
    },
    function(res) {
      let data = res.data
      that.setData({
        startDate: data.timeConf.startDate,
        levels: data.level,  
        selLevelIndex: data.selLevelIndex,
        stepDay: data.timeConf.stepDay      
      })
    })
  },

  /**
   * 保存设置
   */
  saveConf(){
    let that = this

    lbbTaskService.setTaskTimeConf({
      id: that.data.confId,
      startDateStr: that.data.startDate, 
      stepDay: that.data.stepDay,
      selLevelIndex: that.data.selLevelIndex
    }, function (res){

     wx.showToast({
       title: res.result == 0 ? '更新完成' : res.data.description,
     })

     if(res.result == 0) {
       that.setData({
         levels: res.data
       })
     }

    })
  },

  delConf(){
    let that = this

    lbbTaskService.delTaskTimeConfById({
      confId: that.data.confId,
    }, function (res){

     wx.showToast({
       title: res.result == 0 ? '删除完成' : res.data.description,
     })
     if(res.result == 0) {
      wx.navigateBack({
        delta: 1
      })
     }

    })

  }

})